package com.motu.monstercity.server.game.logic;

import com.hazelcast.internal.server.Server;
import com.motu.monstercity.server.game.commondata.feast.FeastConstant;
import com.motu.monstercity.server.game.commondata.feast.FeastMain;
import com.motu.monstercity.server.game.userdata.*;
import com.motu.vertx.module.utility.model.EntityManager;
import com.motu.vertx.module.utility.model.Model;
import com.motu.vertx.module.utility.toolset.TimeUtils;

import java.util.ArrayList;
import java.util.List;

public class FeastUserData extends FeastBaseData {
    public static UserFeastInfo getUserFeastInfo(long userId) {
        return (UserFeastInfo) EntityManager.getUniqueEntity(UserFeastInfo.class, userId);
    }

    public static ServerFeast getServerFeast(int serverId, long userId, int feastMainId) {
        String uniqkey = FeastManager.getFeastUniqKey(userId, feastMainId);
        return (ServerFeast) EntityManager.getEntity(ServerFeast.class, serverId, uniqkey);
    }

    public static ServerFeast getServerFeastByUniqKey(int serverId, String uniqKey) {
        return (ServerFeast) EntityManager.getEntity(ServerFeast.class, serverId, uniqKey);
    }

    public static ServerFeastSeat getServerFeastSeat(long feastId, long userId) {
        return (ServerFeastSeat) EntityManager.getEntity(ServerFeastSeat.class, feastId, userId);
    }

    // 根据dbid 获取UserFeastRecord
    public static UserFeastRecord getUserFeastRecord(long userId, long dbid) {
        return (UserFeastRecord) EntityManager.getEntity(UserFeastRecord.class, userId, dbid);
    }

    // 根据dbid 获取feastchatrecord
    public static FeastChatRecord getFeastChatRecord(long feastId, long dbid) {
        return (FeastChatRecord) EntityManager.getEntity(FeastChatRecord.class, feastId, dbid);
    }

    // 根据serverfeast的dbid 获取弹窗记录
    public static UserFeastPopup getUserFeastPopup(long userId, long feastDbid) {
        return (UserFeastPopup) EntityManager.getEntity(UserFeastPopup.class, userId, feastDbid);
    }

    // 获取所有的盛典派对弹窗记录
    public static List<UserFeastPopup> getUserFeastPopupList(long userId) {
        List<UserFeastPopup> list = new ArrayList<>();
        for (Model model : EntityManager.getEntityList(UserFeastPopup.class, userId)) {
            UserFeastPopup entity = (UserFeastPopup) model;
            if (TimeUtils.getCurTime() - entity.getPopupTime() > TimeUtils.DAY) {
                entity.delete();// 删除超过1天的记录
            } else {
                list.add(entity);
            }
        }
        return list;
    }

    // 获取所有派对，包括已结束的
    public static List<ServerFeast> getAllServerFeastList(int serverId) {
        List<ServerFeast> list = new ArrayList<>();
        for (Model model : EntityManager.getEntityList(ServerFeast.class, serverId)) {
            ServerFeast entity = (ServerFeast) model;
            list.add(entity);
        }
        return list;
    }

    // 获取本服的派对列表
    public static List<ServerFeast> getServerFeastList(int serverId) {
        List<ServerFeast> list = new ArrayList<>();
        for (Model model : EntityManager.getEntityList(ServerFeast.class, serverId)) {
            ServerFeast entity = (ServerFeast) model;
            FeastMain feastMain = getFeastMain(entity.getFeastMainId());
            long curTime = TimeUtils.getCurTime();
            if (entity.getAddTime() + feastMain.getDurationTime() > curTime) {// 未结束的派对
                list.add(entity);
            } else {
                if (entity.getAddTime() + feastMain.getDurationTime() > curTime + TimeUtils.DAY) {
                    entity.delete();// 删除超过一天的派对
                }
            }
        }
        return list;
    }

    // 获取某派对的座位列表
    public static List<ServerFeastSeat> getServerFeastSeatList(long feastId) {
        List<ServerFeastSeat> list = new ArrayList<>();
        for (Model model : EntityManager.getEntityList(ServerFeastSeat.class, feastId)) {
            ServerFeastSeat entity = (ServerFeastSeat) model;
            list.add(entity);
        }
        return list;
    }

    // 获取某派对的聊天内容列表
    public static List<FeastChatRecord> getFeastChatRecordList(long feastId) {
        List<FeastChatRecord> list = new ArrayList<>();
        for (Model model : EntityManager.getEntityList(FeastChatRecord.class, feastId)) {
            FeastChatRecord entity = (FeastChatRecord) model;
            list.add(entity);
        }
        return list;
    }

    // 获取举办记录列表
    public static List<UserFeastRecord> getUserFeastRecordList(long userId) {
        List<UserFeastRecord> list = new ArrayList<>();
        for (Model model : EntityManager.getEntityList(UserFeastRecord.class, userId)) {
            UserFeastRecord entity = (UserFeastRecord) model;
            list.add(entity);
        }
        return list;
    }

    // 某个普通派对的参与记录
    public static List<UserFeastJoinDetail> getUserFeastJoinDetailList(long feastId) {
        List<UserFeastJoinDetail> list = new ArrayList<>();
        for (Model model : EntityManager.getEntityList(UserFeastJoinDetail.class, feastId)) {
            UserFeastJoinDetail entity = (UserFeastJoinDetail) model;
            list.add(entity);
        }
        return list;
    }
}
